package com.fairytask.ui.skillUnit
{
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.text.Text;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.MainSkillUI;
	import com.fairytask.ui.base.UnitUI;
	import com.fairytask.ui.item.SkillItem;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * 技能UI 
	 * @author xujh
	 * 
	 */
	public class SkillUI extends UnitUI
	{
		private static var _self	:SkillUI;
		
		public var infoBg		:MovieClip;	//技能信息背景
		public var bigImgBg	:MovieClip;	//技能大图背景
		
		public var skillVG		:ViewGroup;	//所有的技能容器
		public var useVG		:ViewGroup;	//使用的技能
		public var bigImg		:Clip;		//技能大图片
		public var levelLab	:Label;		//技能等级
		public var sprNum		:Text;		//需要的灵力值
		public var upgradeBtn	:Button;	//升级按钮
		public var upAllBtn	:Button;	//一键升满
		public var pickupBtn	:Button;	//装备按钮
		public var takeoffBtn	:Button;	//卸下按钮
		public var infoSpace	:Sprite = new Sprite();
		//==================数据=====================
		private var _unbind	:Array	= [];	//绑定的属性
		private var _bindUse	:Array	= [];	//使用
		private var _curPos	:int	= -1;	//当前位置
//		private var _data		:BaseData;		//数据
//		private var _curArr	:Array	= [];	//选中对象绑定
		private var _curSkill	:int;			//当前技能
		private var _isFull	:Boolean = false;	//全满待选位置状态
		private var _offPos	:int	= -1;	//卸载位置
		private var _needSpirit	:int;		//需要的灵气
		private var _hasSpirit		:int;		//现有灵气
		
		public static function getInstance():SkillUI
		{
			if(null	==	_self)
			{
				_self	=	new SkillUI();				
			}
			return _self;
		}
		
		public function SkillUI()
		{
			super();
			//给他个名字用来在引导中获取
			upgradeBtn.name	=	"upgradeBtn";
			
			initSkill();
			initEvent();
			initBind();
			pickupBtn.visible = false;
			takeoffBtn.visible = false;
			infoSpace.x = -2;
			infoSpace.y = 55;
			windowBG.addChild(infoSpace);
			//默认选中第一个
			onSelect(0);
		}
		
		/**
		 * 初始化事件
		 * 
		 */
		private function initEvent():void
		{
			//技能列表点击
			skillVG.addItemsEvent(MouseEvent.CLICK,onClick);
			skillVG.addItemsEvent(MouseEvent.MOUSE_OVER,onSkillOver);
			skillVG.addItemsEvent(MouseEvent.MOUSE_OUT,onOut);
			useVG.addItemsEvent(MouseEvent.CLICK,onChange);
			useVG.addItemsEvent(MouseEvent.MOUSE_OVER,onUseOver);
			useVG.addItemsEvent(MouseEvent.MOUSE_OUT,onOut);
			//装备
			pickupBtn.addEventListener(MouseEvent.CLICK,onPickUP);
//			GameStage.stage.addEventListener(MouseEvent.MOUSE_UP,hideBtn);
			MainSkillUI.getInstance().addEventListener(MouseEvent.MOUSE_UP,hideBtn);
			//卸下
			takeoffBtn.addEventListener(MouseEvent.CLICK,onTakeOff);
			//升级
			upgradeBtn.addEventListener(MouseEvent.CLICK,onUpgrade);
			//一键升级
			upAllBtn.addEventListener(MouseEvent.CLICK,onUpAll);
			//全满的时候
			//model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_USESKILL_BOX_FULL,onFull);
		}
		
		/**
		 * 绑定数据 
		 * 
		 */
		private function initBind():void
		{
			//绑定技能
			/*bindManager.BindView(model.scene, ViewDefine.VIEWPORT_SKILL, onAddSkill, onDelSkill);
			//绑定装备技能
			bindManager.BindView(model.scene, ViewDefine.VIEWPORT_USESKILL, onAddUse, onRemoveUse);
			//绑定数据
			bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Spirit",compareSpirit);*/
		}
		
		/**
		 * 现有灵气
		 * @param key
		 * @param val
		 * 
		 */
		private function compareSpirit(key:String,val:int):void
		{
			_hasSpirit = val;
			setSpiritColor();
		}
		
		/**
		 * 设置所需灵气文字颜色 
		 * 
		 */
		private function setSpiritColor():void
		{
			//灵不足为红色
			if(_needSpirit > _hasSpirit)
			{
				sprNum.color = "FF0000";
//				upgradeBtn.enabled = false;
//				upAllBtn.enabled = false;
			}
			else
			{
				sprNum.color = "FFFFFF";
//				upgradeBtn.enabled = true;
//				upAllBtn.enabled = true;
			}
		}
		
		/**
		 * 初始化技能并加锁 
		 * 
		 */
		private function initSkill():void
		{
			for(var i:int=0;i<12;i++)
			{
				var tmpItem	:SkillItem = new SkillItem();
				var baseItem:BaseItem = skillVG.getItemByIndex(i);
				//锁定
				tmpItem.lockSkill = true;
				tmpItem.pos = i;
				tmpItem.data = null;
				tmpItem.skillId = null;
//				tmpItem.addEventListener(MouseEvent.CLICK,onClick);
				baseItem.addItem(tmpItem);
			}
			for(i=0;i<5;i++)
			{
				tmpItem = new SkillItem();
				baseItem = useVG.getItemByIndex(i);
				tmpItem.name = "UseSkill";
				tmpItem.pos = i;
				tmpItem.data = null;
				tmpItem.skillId = null;
				baseItem.addItem(tmpItem);
			}
		}
		
		/**
		 * 清除技能，并加锁 
		 * @param pos
		 * 
		 */
		private function clearSkill(vg:ViewGroup,pos:int):void
		{
			var tmpItem	:SkillItem = vg.getItemByIndex(pos).getItem() as SkillItem;
			//清除内容
			tmpItem.data = null;
			tmpItem.skillId = null;
			tmpItem.filters = null;
			//清除图片
			tmpItem.clearPhoto();
			if(vg == skillVG)
			{
				//锁定
				tmpItem.lockSkill = true;
				//解绑属性
				unBindData(_unbind,pos);
			}
			else
			{
				unBindData(_bindUse,pos);
			}
		}
		
		private function clearInfo():void
		{
			bigImg.clear();
			levelLab.htmlStr = "";
			sprNum.htmlStr = "";
		}
		
		/**
		 * 添加技能 
		 * @param key
		 * @param val
		 * @param data
		 * 
		 */
		private function onAddSkill(key:String, val:ViewObject, data:Object):void
		{
			var _skillId	:String	= val.GetProperties("ConfigID");
			var pos			:int	= val.GetProperties("Pos")-1;
			//判断是否是基本技能
			/*var commonSkill:int=int(StaticIni.getIniVar(_skillId,"CommonSkill"));
			if(commonSkill)
			{
				return;
			}*/
			//判断技能是否大于11个
			if(pos>=13)
			{
				return;
			}
			var tmpItem	:SkillItem = skillVG.getItemByIndex(pos).getItem() as SkillItem;
			_unbind[pos] = [val,"ConfigID",bindManager.BindObjProp(val,"ConfigID",setLevelNum,[pos,skillVG])];
//			_unbind[pos] = [val,"Level",bindManager.BindObjProp(val,"Level",setLevelNum,[pos])];
			tmpItem.source = ResConfig.iconPath +StaticIni.getIniVar(_skillId,"Icon") + ResConfig.itemImg;
			tmpItem.skillId = _skillId;
			tmpItem.data = val;
		}
		
		/**
		 * 删除技能 
		 * @param key
		 * @param val
		 * @param data
		 * 
		 */
		private function onDelSkill(key:String, val:ViewObject, data:Object):void
		{
			var pos:int			= val.GetProperties("Pos")-1;
			clearSkill(skillVG,pos);
		}
		
		/**
		 * 添加使用技能 
		 * @param key
		 * @param val
		 * 
		 */
		private function onAddUse(key:String, val:ViewObject, data:Object):void
		{
			var _skillId	:String	= val.GetProperties("ConfigID");
			var pos			:int	= val.GetProperties("Pos")-1;
			if(pos>4)
			{
				return;
			}
			var tmpItem	:SkillItem = useVG.getItemByIndex(pos).getItem() as SkillItem;
			_bindUse[pos] = [val,"ConfigID",bindManager.BindObjProp(val,"ConfigID",setLevelNum,[pos,useVG])];
			tmpItem.source = ResConfig.iconPath +StaticIni.getIniVar(_skillId,"Icon") + ResConfig.itemImg;
			tmpItem.skillId = _skillId;
			tmpItem.data = val;
		}
		
		/**
		 * 移除使用技能 
		 * @param key
		 * @param val
		 * 
		 */
		private function onRemoveUse(key:String, val:ViewObject, data:Object):void
		{
			var pos:int			= val.GetProperties("Pos")-1;
			if(pos>4)
			{
				return;
			}
			clearSkill(useVG,pos);
		}
		
		/**
		 * 绑定等级更改时 
		 * 
		 */
		private function setLevelNum(key:String,skillID:int,pList:Array):void
		{
			var tmpItem	:SkillItem = pList[1].getItemByIndex(pList[0]).getItem() as SkillItem;
			var level	:int = int(StaticIni.getIniVar(skillID,"Level"));
			//当前位置改变时,当前数据更新
			if(_curPos == pList[0])
			{
				_curSkill = skillID;
				setInfo(_curSkill);
			}
			tmpItem.skillId = String(skillID);
			tmpItem.level = level;
			if(level != 0)
			{
				tmpItem.filters = null;
			}
			else
			{
				tmpItem.filters = [Filter.GRAY];
			}
		}
		
		/**
		 * 解绑相关属性 
		 * @param pos
		 * 
		 */
		private function unBindData(arr:Array,pos:int):void
		{
			if(arr[pos] == null)
			{
				return;
			}
			if(arr[pos].length>0)
			{
				//解绑数据
				bindManager.UnbindProp(arr[pos][0],arr[pos][1],arr[pos][2]);
				//数组清空
				arr[pos] = [];
			}
		}
		
		/**
		 * 单击弹出装备按钮 
		 * @param evt
		 * 
		 */
		private function onClick(evt:MouseEvent,pos:int):void
		{
			var tmpItem	:SkillItem = skillVG.getItemByIndex(pos).getItem() as SkillItem;
			if(null != tmpItem.data)
			{
				//!=0可以装备
				if(tmpItem.level!=0)
				{
					//显示装备按钮
					pickupBtn.visible = true;
					pickupBtn.x = skillVG.x + skillVG.getItemByIndex(pos).x + 35;
					pickupBtn.y = skillVG.y + skillVG.getItemByIndex(pos).y + 35;
				}
//				pickupBtn.x = evt.currentTarget.x + int(evt.localX);
//				pickupBtn.y = evt.currentTarget.y + int(evt.localY);
				//选中位置相同不做操作
				if(_curPos == pos)
				{
					return;
				}
				onSelect(pos);
			}
		}
		
		/**
		 * 显示技能tips 
		 * @param evt
		 * @param pos
		 * 
		 */
		private function onSkillOver(evt:MouseEvent,pos:int):void
		{
			if(null != (skillVG.getItemByIndex(pos).getItem() as SkillItem).skillId)
			{
				showTips(skillVG,pos);
			}
			//未开启
			else
			{
				var level	:String = StaticIni.getIniVar("PlayerSkill_"+(pos+1),"Level");
				if(level != "")
				{
					TipsManage.showTips(StaticIni.getSwfLangStrVar("PlayerSkillLcok",[level]));
				}
			}
		}
		
		/**
		 * 显示装备技能tips 
		 * @param evt
		 * @param pos
		 * 
		 */
		private function onUseOver(evt:MouseEvent,pos:int):void
		{
			showTips(useVG,pos);
		}
		
		/**
		 * 显示对应位置tips内容 
		 * @param vg 视图
		 * @param pos 位置
		 * 
		 */
		private function showTips(vg:ViewGroup,pos:int):void
		{
			var tmpItem	:SkillItem = vg.getItemByIndex(pos).getItem() as SkillItem;
			if(null != tmpItem.skillId)
			{
				TipsManage.showSkillTips(uint(tmpItem.skillId));
			}
		}
		
		/**
		 * 隐藏tips 
		 * @param evt
		 * @param pos
		 * 
		 */
		private function onOut(evt:MouseEvent,pos:int):void
		{
			ToolTips.hide();
		}
		
		/**
		 * 
		 * @param evt
		 * @param pos
		 * 
		 */
		private function onChange(evt:MouseEvent,pos:int):void
		{
			if(_isFull)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PREPARE_SKILL,1,RoleListUI.getInstance().curObj,_curSkill,pos+1);
				clearFilter();
				_isFull = false;
				return;
			}
			//出现卸载按钮
			var tmpItem :SkillItem = useVG.getItemByIndex(pos).getItem() as SkillItem;
			if(tmpItem.data == null)
			{
				return;
			}
			_offPos = pos + 1;
			takeoffBtn.visible = true;
			takeoffBtn.x = useVG.x + useVG.getItemByIndex(pos).x + 35;
			takeoffBtn.y = useVG.y + useVG.getItemByIndex(pos).y + 35;
		}
		
		/**
		 * 选中位置 
		 * @param pos
		 * 
		 */
		private function onSelect(pos:int):void
		{
			//不是第一次选中，清除原先选中的效果
			if(_curPos != -1)
			{
				removeSelect();
			}
			var tmpItem :SkillItem = skillVG.getItemByIndex(pos).getItem() as SkillItem;
			if(tmpItem.data == null)
			{
				return;
			}
			_curPos = pos;
			_curSkill = int(tmpItem.skillId);
			//绑定数据
//			var _obj	:ViewObject = tmpItem.data as ViewObject;
//			_curArr = [tmpItem.data,"ConfigID",bindManager.BindPropInViewObj(model.scene.GetView(ViewDefine.VIEWPORT_SKILL),_obj.ObjKey,"ConfigID",setInfo,null)];
			setInfo(_curSkill);
			//选中效果
			skillVG.getItemByIndex(pos).filters = [Filter.YELLOW_INNER];
			
		}
		
		private function removeSelect():void
		{
			//删除滤镜
			skillVG.getItemByIndex(_curPos).filters = null;
			//解绑数据
//			bindManager.UnbindProp(_curArr[0],_curArr[1],_curArr[2]);
//			_curArr = [];
			_curPos = -1;
			_curSkill = 0;
			
		}
		
		/**
		 * 绑定技能 
		 * @param val skillID
		 * 
		 */
		private function setInfo(val:int):void
		{
			//添加下面详细信息内容
			bigImg.source = ResConfig.iconPath +StaticIni.getIniVar(val,"Icon") + ResConfig.bigImg;
			bigImg.width = 80;
			bigImg.height = 80;
			levelLab.htmlStr = StaticIni.getSwfLangStr("1000000062") + StaticIni.getIniVar(val,"Level");
			_needSpirit = int(StaticIni.getIniVar(val,"ConsumeSpirit"));
			sprNum.htmlStr = StaticIni.getSwfLangStrVar("1000000063",[_needSpirit]);
			setSpiritColor();
		}
		
		/**
		 * 点击其他地方隐藏菜单
		 * @param evt
		 * 
		 */		
		private function hideBtn(evt:MouseEvent):void
		{
			pickupBtn.visible = false;
			takeoffBtn.visible = false;
			if(null!=evt.target.parent)
			{
				if(evt.target.parent.name == "UseSkill")
				{
					return;
				}
			}
			clearFilter();
			_isFull = false;
		}
		
		/**
		 * 取消高亮 
		 * 
		 */
		private function clearFilter():void
		{
			for(var i:int=0;i<5;i++)
			{
				var tmpItem	:SkillItem = useVG.getItemByIndex(i).getItem() as SkillItem;
				tmpItem.filters = null;
			}
		}
		
		/**
		 * 全部高亮 
		 * 
		 */
		private function addFilter():void
		{
			for(var i:int=0;i<5;i++)
			{
				var tmpItem	:SkillItem = useVG.getItemByIndex(i).getItem() as SkillItem;
				tmpItem.filters = [Filter.YELLOW_INNER];
			}
		}
		
		/**
		 * 单击装备
		 * @param evt
		 * 
		 */
		private function onPickUP(evt:MouseEvent):void
		{
			if(_curSkill != 0)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PREPARE_SKILL,0,RoleListUI.getInstance().curObj,_curSkill);
			}
		}
		
		/**
		 * 单击卸载
		 * @param evt
		 * 
		 */
		private function onTakeOff(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REMOVE_SKILL,_offPos,RoleListUI.getInstance().curObj);
		}
		
		/**
		 * 单击升级
		 * @param evt
		 * 
		 */
		private function onUpgrade(evt:MouseEvent):void
		{
			if(_curSkill != 0)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LEARN_SKILL,_curSkill,0,RoleListUI.getInstance().curObj);
			}
		}
		
		/**
		 * 单击一键升级
		 * @param evt
		 * 
		 */
		private function onUpAll(evt:MouseEvent):void
		{
			if(_curSkill != 0)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LEARN_SKILL,_curSkill,1,RoleListUI.getInstance().curObj);
			}
		}
		
		private function onFull(evt:CustomToClientEvent):void
		{
			_isFull = true;
			//选择位置的提示
			//全部高亮
			addFilter();
		}
		
		/**
		 * 重置 
		 * 
		 */
		override public function reset():void
		{
			//装备隐藏
			pickupBtn.visible = false;
		}
	}
}